dojo.provide("uws.drawing.DrawManager");

dojo.declare('uws.drawing.DrawManager',null,{
	/*
		Given the mouse  events, it determines how a component will be drawn.
	*/
	drawing:null,
	surface:null,
	diagramType:null,
	componentType:null,
	button:null,
	_e:null,
	
	constructor: function(){},
	
	drawOnClick:function(button,toDraw,diagramType,componentType){
		var surface = surfaceManager.getVisibleSurface();
		if(toDraw && surface!=null){
			this.drawing = true;
			this.surface = surface;
			this.diagramType = diagramType;
			this.componentType = componentType;
			this.button = button;
		}else if(surface!=null){
			this.drawing = false;
			this.button = null;
			this.surface = null;
			this.diagramType = null;
			this.componentType = null;
		}
	},
	drawNow:function(e){
		/*
			Sample custom attribute
			attrName#attrFormalName#value#isRequired#dataType#defaultValue#restriction1=value;retriction2=value 
		*/
		if(this.drawing){
			if(this.diagramType=="usecase"){
				if(this.componentType=="actor"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("actor", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "attrName#attrFormalName##false#Text##");
							var comp = new uws.drawing.usecase.Actor(this.surface, id, this._e.layerX, this._e.layerY, sortOrder, "");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="usecase"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("usecase", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.usecase.UseCase(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="systembox"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("systembox", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.usecase.SystemBox(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="note"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("note", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.usecase.Note(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}
			}else if(this.diagramType=="classdiagram"){
				if(this.componentType=="classbox"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("classBox", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.classdiagram.ClassBox(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}
			}else if(this.diagramType=="activity"){
				if(this.componentType=="activity"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("activity", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.activity.Activity(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="decision"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("decision", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.activity.Decision(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="start"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("start", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.activity.Start(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="stop"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("stop", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.activity.Stop(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}
			}else if(this.diagramType=="sequence"){
				if(this.componentType=="objectComponent"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("objectComponent", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.sequence.ObjectComponent(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}
			}else if(this.diagramType="sqlerd"){
				if(this.componentType=="entity"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("entity", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.sqlerd.Entity(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="wEntity"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("wEntity", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "");
							var comp = new uws.drawing.sqlerd.WeakEntity(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="attribute"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("attribute", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "datatype#Data Type#VARCHAR(45)#true#String#varchar(45)#|not_null#Not Null#false#false#Boolean#false#");
							var comp = new uws.drawing.sqlerd.Attribute(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="kAttribute"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("kAttribute", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "autoincrement#Auto Increment#true#false#Boolean#true#|datatype#Data Type#INT#true#String#varchar(45)#");
							var comp = new uws.drawing.sqlerd.KeyAttribute(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="mAttribute"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("mAttribute", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "datatype#Data Type#VARCHAR(45)#true#String#varchar(45)#|not_null#Not Null#false#false#Boolean#false#");
							var comp = new uws.drawing.sqlerd.MultivaluedAttribute(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}else if(this.componentType=="dAttribute"){
					this._e=e;
					dataStore.fetchItemByIdentity({
						identity: this.surface.id,
						scope:this,
						onItem:function(diagram){
							var sortOrder = sortOrderManager.getSortOrder(this.surface.id);
							var id = componentManager.newItem("dAttribute", this._e.layerX, this._e.layerY, sortOrder, "", '', diagram, "datatype#Data Type#VARCHAR(45)#true#String#varchar(45)#|not_null#Not Null#false#false#Boolean#false#");
							var comp = new uws.drawing.sqlerd.DerivedAttribute(this.surface, id, this._e.layerX, this._e.layerY, sortOrder,"");
							componentManager.addItem(id,comp);
							
							console.dir(componentManager.components);
							
							this.button.set('checked',false);
							componentFocusManager.focus(comp);
							this._e=null;
						},
						onError:function(error){
							console.debug(error);
							this._e=null;
						}
					});
				}
			}
			
			
			this.drawing = false;
			this.button = null;
			this.surface = null;
			this.diagramType = null;
			this.componentType = null;
		}
	},
	drawFromDataStore:function(parent, item){
		var diagtype = parent.umltype[0];
		var comptype = item.umltype[0];
		
		if(diagtype=="usecase"){
			if(comptype=="actor"){
				var comp = new uws.drawing.usecase.Actor(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="usecase"){
				var comp = new uws.drawing.usecase.UseCase(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="note"){
				var comp = new uws.drawing.usecase.Note(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="systembox"){
				var comp = new uws.drawing.usecase.SystemBox(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}
		}else if(diagtype=="classdiagram"){
			if(comptype=="classBox"){
				var comp = new uws.drawing.classdiagram.ClassBox(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}
		}else if(diagtype=="activity"){
			if(comptype=="activity"){
				var comp = new uws.drawing.activity.Activity(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="decision"){
				var comp = new uws.drawing.activity.Decision(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="start"){
				var comp = new uws.drawing.activity.Start(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="stop"){
				var comp = new uws.drawing.activity.Stop(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}
		}else  if(diagtype=="sequence"){
			if(comptype=="objectComponent"){
				var comp = new uws.drawing.sequence.ObjectComponent(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}
		}else  if(diagtype=="sqlerd"){
			if(comptype=="entity"){
				var comp = new uws.drawing.sqlerd.Entity(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="wEntity"){
				var comp = new uws.drawing.sqlerd.WeakEntity(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp)
			}else if(comptype=="attribute"){
				var comp = new uws.drawing.sqlerd.Attribute(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="kAttribute"){
				var comp = new uws.drawing.sqlerd.KeyAttribute(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="mAttribute"){
				var comp = new uws.drawing.sqlerd.MultivaluedAttribute(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}else if(comptype=="dAttribute"){
				var comp = new uws.drawing.sqlerd.DerivedAttribute(surfaceManager.getSurface(parent.id[0]), item.id[0], item.x[0], item.y[0]-35, item.sortOrder[0], item.label[0], true);
				componentManager.addItem(item.id[0],comp);
			}
		}
	}
});
